local formulas={}

-- 货1：我卖出的价格 --modulus流行滞销系数
function formulas.getGoods1BuyPrice( defPrice,  modulus)
    return defPrice*(1+modulus)
end

-- 货1：我买入的价格
function formulas.getGoods1SellPrice( defPrice,  modulus,  distance)
    return defPrice*(1+distance/100)*(1+modulus)
end

-- 货物2的默认存储量
function formulas.getGoods2DefaultStore( a,  lastDayLogins)
    lastDayLogins = math.max(100, lastDayLogins)
    return math.modf(a * lastDayLogins)
end

-- 货物2的默认价格
function formulas.getGoods2DefaultPrice( price,  distance)
    return price * (1 + distance / 100.0)
end

-- 货2：我卖出的价格
function formulas.getGoods2SellPrice( defPrice,  defStore,  curStore,  num)
    return formulas.getGoods2Price(defPrice, defStore, curStore, num)
end

-- 货2：我买入的价格
function formulas.getGoods2BuyPrice( defPrice,  defStore,  curStore,  num)
    return -formulas.getGoods2Price(defPrice, defStore, curStore, -num)
end

--货2的成交价
function formulas.getGoods2Price( defPrice,  defStore,  curStore,  num)
    local perstore = math.modf(math.max(defStore / 10.0, 1))
    local n = 0
    for  i = 0,10 do
        if curStore >= i * perstore then
            n = i
        else
            break
        end
    end
    local cansellnum = 0
    if (num >= 0) then--卖
        if (n == 10) then
            cansellnum = num
        else
            cansellnum = math.min(num, math.max(1, (n + 1) * perstore - curStore))
        end
    else --买
        if (num < 0 and curStore == n * perstore) then
            n = math.max(0, n - 1)
        end
        if (n == 0) then
            cansellnum = num
        else
            cansellnum = math.max(num, math.min(-1, n * perstore - curStore))
        end
    end
    num =num- cansellnum
    local peroffset = string.format("%.1f", math.max(0.1, math.min(math.modf(curStore / perstore),10 )))
    local price = math.modf(math.max(1, defPrice / peroffset)) * cansellnum
    curStore =curStore + cansellnum
    if (num == 0) then
        return price
    end
    return price + formulas.getGoods2Price(defPrice, defStore, curStore, num)
end
return formulas
